﻿using BackTask.DashBoard.Models;
using BackTask.Model;
using Quartz;

namespace BackTask.DashBoard.Manager
{
    public class Store
    {
        public ScheduledJob AddJob(IScheduler scheduler, IJobDetail jobDetail, ITrigger trigger, object jobExecuteDetail)
        {
            JobKey key = jobDetail.Key;
            string description = jobDetail.Description;
            var startTime = trigger.StartTimeUtc;
            var nextFireTime = trigger.GetNextFireTimeUtc();
            var previousFireTime = trigger.GetPreviousFireTimeUtc();
            var endFireTime = trigger.EndTimeUtc;
            var state = scheduler.GetTriggerState(trigger.Key).Result;
            ScheduledJob scheduledJob = new ScheduledJob()
            {
                Name = key.Name,
                Description = description,
                GroupName = key.Group,
                StartTime = startTime,
                EndTime = endFireTime,
                PreviousFireTime = previousFireTime,
                NextFireTime = nextFireTime,
                State = state,
                Type = jobExecuteDetail.GetType() == typeof(CronTaskJob) ? TaskBuilderTypes.Cron.ToString().ToLower() : TaskBuilderTypes.Simple.ToString().ToLower(),
                Url = jobDetail.JobDataMap.GetString("url"),
                JobDetail = jobExecuteDetail
            };

            return scheduledJob;
        }
    }
}
